package com.example.hotelmanagement.controller;

import com.example.hotelmanagement.entity.Result;
import com.example.hotelmanagement.entity.User;
import com.example.hotelmanagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result getAllUsers() {
        return Result.ok(userService.findAll());
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        User user = userService.findById(id);
        return user != null ? Result.ok(user) : Result.error();
    }

    @PostMapping
    public Result createUser(@RequestBody User user) {
        return Result.ok(userService.save(user));
    }

    @PutMapping
    public Result updateUser(@RequestBody User user) {
        return Result.ok(userService.update(user));
    }

    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        return Result.ok(userService.delete(id));
    }
}